let pageProps = null;
let isInit = false;
let pickerProps = null;

const checkRewrite = newProps => {
	if (!ecodeSDK.checkLPath('/spa/workflow/static4form/index.html#/main/workflow/req')) return;
	// 过滤签字意见的分页组件
	if (newProps.defaultCurrent !== undefined || newProps.weaSimple) return
	return true
}

// 重写分页，设置成单页模式
const rewritePage = () => {
	if (pageProps === null) return
	// 每页显示的行数
	let pageSize = 1;
	if (pageProps.pageSize === pageSize && !isInit) {
		isInit = true
	}
	// 判断明细表是否加载完毕
	const allTable = document.querySelectorAll('table[name^=oTable]').length
	if (allTable && allTable.length === 0) return
	if (pageProps.pageSize !== pageSize && !isInit) {
		const { onShowSizeChange, current } = pageProps
		if (onShowSizeChange && typeof onShowSizeChange === 'function') {
			onShowSizeChange(current, pageSize);
			isInit = true
		}
	}
}

// 重写明细表分页组件
ecodeSDK.overwritePropsFnQueueMapSet('Pagination', {
	fn: (newProps) => {
		if (!checkRewrite(newProps)) return
		pageProps = newProps
		newProps.className = 'yf_tab_page'
		newProps.showSizeChanger = false
		newProps.defaultPageSize = 1
		return newProps;
	},
});


const rewritePicker = () => {
	if (pickerProps === null) return
	pickerProps.onChange([1])
}

ecodeSDK.overwriteMobilePropsFnQueueMapSet('Picker', {
	fn: (newProps) => {
		if (ecodeSDK.checkLPath('/spa/workflow/static4mobileform/index.html#/req')) {
			pickerProps = newProps
			newProps.data = [{value: 1, label: '1条/页'}]
			return newProps
		}
	}
})


const render = (proxyField, props) => {
	WfForm.isMobile() ? rewritePicker() : rewritePage()
	const acParams = {
		appId: '${appId}',
		name: 'TabDetailRoot',
		isPage: true,
		noCss: true,
		props
	}
	let TabDetailRoot = ecodeSDK.getAsyncCom(acParams)
	GForm.proxyFieldComp(proxyField, <TabDetailRoot />)
}

window.GRender = {
	...window.GRender,
	TabDetail: {
		render,
		getTabStore: () => ecodeSDK.getCom('${appId}', 'tabStore'),
		getRelateStore: () => ecodeSDK.getCom('${appId}', 'relateStore')
	}
}